package com.example.yuejian11.activity;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.example.yuejian11.R;
import com.example.yuejian11.adapter.STAdapter;
import com.example.yuejian11.bean.STEntity;
import com.example.yuejian11.view.TitleView;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;

import java.util.List;

import butterknife.BindView;

public class FTActivity extends BaseActivity {

    private static final String TAG = "STActivity";
    @BindView(R.id.recy)
    RecyclerView recyclerView;
    @BindView(R.id.title_activity)
    TitleView titleView;
    Handler handler_activity = new mHandler();
    RecyclerView.Adapter adapter;
    LinearLayoutManager layoutManager;
    private Context context;
    private String uid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_ft);
        super.onCreate(savedInstanceState);

        initView();
        initData();

    }

    private void initView() {
        uid = SPUtils.getInstance().getInt("uid") + "";
        context = getBaseContext();
        titleView.getLeftBackTextTv().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActivityUtils.finishActivity(FTActivity.class);
            }
        });
    }

    private void initData() {


        EasyHttp.get("yuejian6/public/index.php/json/my/slideshow").params("user_id", uid)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onSuccess(String data) {

                        Message msg = Message.obtain(); // 实例化消息对象
                        msg.what = 0; // 消息标识
                        msg.obj = data; // 消息内容存放

                        handler_activity.sendMessage(msg);
                    }

                    @Override
                    public void onError(ApiException error) {
                        Log.d(TAG, "onFailed: " + error);
                    }
                });
    }

    // 步骤1：自定义Handler子类（继承Handler类） & 复写handleMessage（）方法
    class mHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);


            List<STEntity> stEntities = null;

            try {
                stEntities = JSON.parseArray(msg.obj.toString(), STEntity.class);
            } catch (Exception e) {
                ToastUtils.showShort("获取数据失败");
                return;
            }


            adapter = new STAdapter(context, stEntities, R.layout.st_item);
            layoutManager = new LinearLayoutManager(context);
            //设置布局管理器
            recyclerView.setLayoutManager(layoutManager);
//设置为垂直布局，这也是默认的
            layoutManager.setOrientation(OrientationHelper.VERTICAL);
//设置Adapter
            recyclerView.setAdapter(adapter);
//设置增加或删除条目的动画
            recyclerView.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.VERTICAL));
            recyclerView.setItemAnimator(new DefaultItemAnimator());

        }
    }


}
